package com.gsm.common.shiro.realm;

import com.gsm.domain.system.resp.ProfileResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Set;

/**
 * @Description: 获取安全数据，构造权限信息
 * @Author: gaoshengmin
 * @CreateDate: 2020/2/13$ 20:58$
 * @UpdateUser: gaoshengmin
 * @UpdateDate: 2020/2/13$ 20:58$
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class CustomRealm extends AuthorizingRealm {


    @Override
    public void setName(String name) {
        super.setName("gsmRealm");
    }

    //授权方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        ProfileResult result = (ProfileResult) principalCollection.getPrimaryPrincipal();
        //获取安全信息
        Set<String> apiPerms = (Set<String>) result.getRoles().get("apis");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(apiPerms);
        return info;
    }

    //认证方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }
}
